# Contributor: V.Krishn <vkrishn4@gmail.com>
# Contributor: Eivind Uggedal <eu@eju.no>
# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=valkey
pkgver=7.2.7
pkgrel=0
pkgdesc="Open source high-performance key/value datastore (fork of Redis)"
url="https://valkey.io/"
arch="all"
license="BSD-3-Clause"
pkgusers="valkey"
pkggroups="valkey"
makedepends="linux-headers openssl-dev"
checkdepends="tcl procps"
install="
	$pkgname.pre-install
	$pkgname.post-install
	$pkgname-compat.pre-install
	"
subpackages="
	$pkgname-benchmark
	$pkgname-cli
	$pkgname-compat::noarch
	$pkgname-openrc
	"
source="https://github.com/valkey-io/valkey/archive/$pkgver/valkey-$pkgver.tar.gz
	valkey.conf.patch
	sentinel.conf.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname-sentinel.initd
	$pkgname-sentinel.confd
	"

case "$CARCH" in
	# FIXME: One test is broken on s390x.
	s390x) options="!check";;
esac

# secfixes:
#   7.2.7-r0:
#     - CVE-2024-31227
#     - CVE-2024-31228
#     - CVE-2024-31449

build() {
	export CFLAGS="$CFLAGS -DUSE_MALLOC_USABLE_SIZE -O2 -flto=auto"
	make USE_JEMALLOC=no \
		MALLOC=libc \
		BUILD_TLS=yes \
		all
}

check() {
	make test
}

package() {
	make install PREFIX=/usr INSTALL_BIN="$pkgdir/usr/bin"

	cd "$pkgdir"

	# NOTE: /etc/valkey and /etc/valkey/sentinel.conf must be writable for valkey,
	# otherwise Sentinel fails to start.
	install -d -m 750 -o valkey -g valkey \
		etc/valkey \
		etc/valkey/valkey.d \
		var/lib/valkey \
		var/lib/valkey/sentinel

	install -D -m 640 -o root -g valkey "$builddir"/valkey.conf etc/valkey/valkey.conf
	install -D -m 640 -o valkey -g valkey "$builddir"/sentinel.conf etc/valkey/sentinel.conf

	install -D -m 755 "$srcdir"/valkey.initd etc/init.d/valkey
	install -D -m 755 "$srcdir"/valkey-sentinel.initd etc/init.d/valkey-sentinel
	install -D -m 644 "$srcdir"/valkey.confd etc/conf.d/valkey
	install -D -m 644 "$srcdir"/valkey-sentinel.confd etc/conf.d/valkey-sentinel

	install -d -m 750 -o valkey -g valkey \
		var/lib/valkey \
		var/lib/valkey/sentinel
}

benchmark() {
	pkgdesc="Valkey benchmarking tool"

	amove usr/bin/valkey-benchmark
}

cli() {
	pkgdesc="Valkey CLI client"

	amove usr/bin/valkey-cli
}

compat() {
	pkgdesc="Valkey command symlinks and system group for compatibility with redis"
	depends="!redis !redict-compat"

	amove usr/bin/redis-*
	rm "$subpkgdir"/usr/bin/redis-benchmark
}

sha512sums="
0b9ea574886c913a79b336b724697a2f274cbb58ea5ba6631fba98ebfcb51699a4b553c84c9e0131645353ad446e0ff9011c42dd3b7b7557fdb4d03dcfaf943e  valkey-7.2.7.tar.gz
a5b567a9ac300fed9f3eb484922b49d3677915c7282ffecd3b71505c685cf1964791f4915308ac31f03851f707646313e72bb35b229cfe81fea4a9b1d8f4009b  valkey.conf.patch
d0311d2bfade7efbfa2bdcc6c74e8e8a151c09c627e30f5cea1826155dcb4f7ca4c1d35aba26bccec933575fadcbe5785e16b4801058fec73c7de5537ffeb09a  sentinel.conf.patch
605c1f39cf5f206e03d19af54412a66732ad3c65d5cb36e5d6cfb9d28779807d2da9c29d01f07e0ba2956446cb6935b66c92844a74b255b6070870b30d7d45d8  valkey.initd
639a007f7e98cf7614d2afb6e109042883030e0bdf4eaf1dc3df3ecab8043d6be30647e1e46295f783efc7baf42dc420b2e5dcf388913efdc5bdf2cd1e418f9f  valkey.confd
dd407cb4047524114b4814a28993978acbe300989acc466ec3809171bf19a1e0562756f7b5954981a087a37437c2c281e4a3667d361cfa8873d1c20d1b900632  valkey-sentinel.initd
926316561f0802b577caee03e7bccc5538dc8270095df95853549fdd519fac8d0e89bc3e563cdba2d7ec0dfe10db5e50b95859549597beec894c820abfdbecee  valkey-sentinel.confd
"
